package org.jeecgframework.web.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.ZyContactsServiceI;
import org.jeecgframework.web.service.ZyEquipmentSpareServiceI;
import org.jeecgframework.web.system.pojo.base.TSRoleUser;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.ZyContactsEntity;
import org.jeecgframework.web.system.pojo.base.ZyEquipmentSpareEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("zyEquipmentSpareService")
@Transactional
public class ZyEquipmentSpareServiceImpl extends CommonServiceImpl implements ZyEquipmentSpareServiceI {

    @Autowired
    private ZyContactsServiceI zyContactsService;

    @Autowired
    private SystemService systemService;
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((ZyEquipmentSpareEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
// 		this.doAddSql((ZyEquipmentSpareEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
// 		this.doUpdateSql((ZyEquipmentSpareEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @return
	 */
 	public boolean doAddSql(ZyEquipmentSpareEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @return
	 */
 	public boolean doUpdateSql(ZyEquipmentSpareEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @return
	 */
 	public boolean doDelSql(ZyEquipmentSpareEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,ZyEquipmentSpareEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{sn}",String.valueOf(t.getSn()));
 		sql  = sql.replace("#{item}",String.valueOf(t.getItem()));
 		sql  = sql.replace("#{top_item_sn}",String.valueOf(t.getTopItemSn()));
 		sql  = sql.replace("#{bp_no}",String.valueOf(t.getBpNo()));
 		sql  = sql.replace("#{type}",String.valueOf(t.getType()));
 		sql  = sql.replace("#{belong_cinema_id}",String.valueOf(t.getZyCinema().getId()));
 		sql  = sql.replace("#{belong_org_id}",String.valueOf(t.getTsDepart().getId()));
 		sql  = sql.replace("#{house}",String.valueOf(t.getHouse()));
 		sql  = sql.replace("#{install_date}",String.valueOf(t.getInstallDate()));
 		sql  = sql.replace("#{base_repair_date}",String.valueOf(t.getBaseRepairDate()));
 		sql  = sql.replace("#{send_date}",String.valueOf(t.getSendDate()));
 		sql  = sql.replace("#{repair_end_date}",String.valueOf(t.getRepairEndDate()));
 		sql  = sql.replace("#{server_state}",String.valueOf(t.getServerState()));
 		sql  = sql.replace("#{remarks}",String.valueOf(t.getRemarks()));
 		sql  = sql.replace("#{is_normal}",String.valueOf(t.getIsNormal()));
 		sql  = sql.replace("#{exception_reason}",String.valueOf(t.getExceptionReason()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
 	
 	public void idandname(HttpServletRequest req, TSUser user) {
		List<TSRoleUser> roleUsers = super.findByProperty(TSRoleUser.class, "TSUser.id", user.getId());
		String roleId = "";
		String roleName = "";
		if (roleUsers.size() > 0) {
			for (TSRoleUser tRoleUser : roleUsers) {
				roleId += tRoleUser.getTSRole().getId() + ",";
				roleName += tRoleUser.getTSRole().getRoleName() + ",";
			}
		}
		req.setAttribute("id", roleId);
		req.setAttribute("roleName", roleName);
	}

    public List<ZyEquipmentSpareEntity> findEquipByCinemaId(String cinemaId) {
        if(oConvertUtils.isNotEmpty(cinemaId)){
            /* order by house.roomNum asc*/
            return findHql("from ZyEquipmentSpareEntity where isSpare=0 and zyCinema.id=?",cinemaId);
        }

        return new ArrayList<ZyEquipmentSpareEntity>();
    }

    public List<ZyEquipmentSpareEntity> findEquipByUserName(String userName) {
        if(oConvertUtils.isNotEmpty(userName)){
            ZyContactsEntity ct = zyContactsService.findContactByUserName(userName);
            if(ct.getBelongCinema() != null){
                return findEquipByCinemaId(ct.getBelongCinema().getId());
            }
        }
        return new ArrayList<ZyEquipmentSpareEntity>();
    }

	@Override
	public List<ZyEquipmentSpareEntity> findEquipBySn(String sn) {
		if(oConvertUtils.isNotEmpty(sn)){
            /* order by house.roomNum asc*/
			return findHql("from ZyEquipmentSpareEntity where sn=?",sn);
		}
		return new ArrayList<ZyEquipmentSpareEntity>();
	}

	@Override
    public String checkBP(String bp) {
 		if(StringUtil.isEmpty(bp)){
 			return "";
		}
        String zyBp = systemService.getTypeByCode("bj_hdbp","zy");
        String hdBp = systemService.getTypeByCode("bj_hdbp","hd");
        if(oConvertUtils.isNotEmpty(zyBp) && zyBp.contains(bp)){
            return "z";
        }
        if(oConvertUtils.isNotEmpty(hdBp) && hdBp.equals(bp)){
            return "h";
        }
        return bp;
    }
}